package com.example.blogsystem.utils;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.util.UUID;
public class SecurityUtils {
    public static void main(String[] args) throws UnsupportedEncodingException {
        //MD5加秘密
        System.out.println(DigestUtils.md5DigestAsHex("123456".getBytes("UTF-8")));
        //盐值
        System.out.println(UUID.randomUUID());
    }
    /*
    * 加密
    * */
    private String password = "123456";
    @Test
    void encrypt() throws UnsupportedEncodingException {
        String salt = UUID.randomUUID().toString().replace("-","");
        //mds
        String secretString = DigestUtils.md5DigestAsHex((salt+password).getBytes("UTF-8"));
        //数据库储存，盐值加密码
        String sqlPassword = salt+secretString;
        System.out.println(sqlPassword);
    }
    @Test
    void verify() throws UnsupportedEncodingException {
        String inputPassword = "123456";
        String sqlPassword = "0818733528054f158de7056c6cc5e9e687c3a04857487955793a25ce515ef714";
        String salt = sqlPassword.substring(0,32);
        String secretString = DigestUtils.md5DigestAsHex((salt+inputPassword).getBytes("UTF-8"));
        boolean result = (salt+secretString).equals(sqlPassword);
        System.out.println(result);
    }
}
